<?php

namespace app\daili\controller;

use app\dsadmin\model\SiyouModel;
use app\dsadmin\model\GongyouModel;
use think\Cache;
use think\Db;
use think\Request;

class Siyou extends Base
{
 public function index()
   {
        // return json(['url' => "请稍后重提,系统升级中,20:00后开放"]);
 
 
        $minfo = Db::name('member')->where('id', session('dailiuid'))->find();
        if($minfo['status_zong'] == 1 && session('dailiuid') != 201168){
            die('总代无法提取链接。。。');
        }
        
        if (input('post.allupdate') == "allupdate")
        {
            $money = input('param.number/f');
            $sj    = input('param.sj');
            
            
           
            
            if($money < 3 || $money >= 10){
                return json(['code' => '0', 'msg' => '金额不能低于3元或高于10元']);
            }
            
            Db::name('pian')->where('userid', session('dailiuid'))->update(['money' => $money]);
            Cache::store('redis')->set("ylmoney".session('dailiuid'),$money,77879680);
          
            return json(['code' => '1', 'msg' => 'ok']);
        }
        
         if (input('post.allupdate') == "allupdatebaotian")
        {
            $money = input('param.number');
            $money_type = input('param.money_type');
            $money = $money_type;
            if($money != 0){
                if($money < 30 || $money > 50){
                      return json(['code' => '0', 'msg' => '金额不能低于30元 并且不能大于50元']);
                }
                
              if($money!=30 && $money!=38 && $money!=39 && $money!=48){
                    return json(['code' => '0', 'msg' => '金额只能为30,38,39,48元。']);
                }
                
            }
             
            
             Db::name('pian')->where('userid', session('dailiuid'))->update(['b_money' => $money]);
            
             $baoinfo = Db::name('pian')->where('userid', session('dailiuid'))->find();
           
             Cache::store('redis')->set(session('dailiuid').'ylbaotian',json_encode($baoinfo),77879680);
          
            return json(['code' => '1', 'msg' => 'ok']);
        }

        if (input('post.alldel'))
        {
            // Db::name('siyou')->where('userid', session('dailiuid'))->where('id', 'neq', '0')->delete();
            return json(['code' => '1', 'msg' => 'ok']);
        }

        $typess = input('type');
        $map = $typess ? ['type' => $typess] : ['userid' => session('dailiuid')];

        $map['userid']   = session('dailiuid');
        $map['type']     = 1;
        $map['dwz_type'] = 0;
        $map['create_time'] = ['>=',strtotime(date("Y-m-d", strtotime("-1 day")))];
        $Nowpage = input('get.page') ? input('get.page') : 1;
        $limits = 20; // 获取总条数
        $count = Db::name('dwz')->where($map)->count(); //计算总页面
        $allpage = intval(ceil($count / $limits));
        $lists = Db::name('dwz')->where($map)->page($Nowpage, 10)->order('id desc')->select();
        $ename_first = Db::name('member')->where('id', session('dailiuid'))->value('ename_first');
        $ename_type = Db::name('ename')->where('ename', $ename_first)->value('ename_type');
        $this->assign('Nowpage', $Nowpage); //当前页
        $this->assign('allpage', $allpage); //总页数
        $this->assign('count', $count);
        if (input('get.page'))
        {
            return json($lists);
        }
        $moneypian = Db::name('pian')->where(['userid' => session('dailiuid')])->value('money');
         $b_money = Db::name('pian')->where(['userid' => session('dailiuid')])->value('b_money');

        $type = db('type')->order('id desc')->select();
        $this->assign('type', $type);
        $this->assign('moneypian', $moneypian);
           $this->assign('b_money', $b_money);
        $this->assign('typess', $typess);
        return $this->fetch();
    }

    public function read()
    {
        $id = input('id');
        $cate = new SiyouModel();
        $shipin = $cate->getOneSiyou($id);
        $this->assign('shipin', $shipin);
        return $this->fetch();
    }
    public function fabushipin()
    {
        
        
        echo "功能维护中";
        die;
        $newnamepath = "uploads/shipin/" . date("Ymd", time()) . "/" . md5(time() . mt_rand(1, 1000000));
        $this->assign('newnamepath', $newnamepath);

        if (input('post.url'))
        {
            $siyoucount = Db::name('siyou')->where(['userid' => session('dailiuid')])->count();
            if ($siyoucount + 1 > config('dlzdsps'))
            {
                return json(['code' => 0, 'msg' => "视频数超过限制"]);
            }

            $data = input('post.');
            $param = $data;
            if (intval($param['issuiji']) == 0)
            {
                if (floatval($param['start']) < config("zuidijiage"))
                {
                    return json(['code' => 0, 'msg' => "价格错误"]);
                }

                if (floatval($param['end']) > config("zuidajiage"))
                {
                    return json(['code' => 0, 'msg' => "价格错误"]);
                }

                if (floatval($param['end']) < floatval($param['start']))
                {
                    return json(['code' => 0, 'msg' => "价格错误"]);
                }
            }

            if (intval($param['issuiji']) == 1 || config('sfkqsims') == 1)
            {
                if (floatval($param['guding']) < config("zuidijiage") || floatval($param['guding']) > config("zuidajiage") || !is_numeric($param['guding']))
                {
                    return json(['code' => 0, 'msg' => "价格错误"]);
                }
                $data['guding'] = floatval($param['guding']);
            }

            $Siyou = new SiyouModel();
            $flag = $Siyou->fabushipin($data);
            return json(['code' => $flag['code'], 'data' => $flag['data'], 'msg' => $flag['msg']]);
        }

        $type = db('type')->order('id desc')->select();
        $this->assign('type', $type);
        return $this->fetch();
    }

    public function wodelianjie()
    {
        if (input('get.shengcheng'))
        {
            $siyou = Db::name('siyou')->where(['userid' => session('dailiuid')])->select();
            if ($siyou)
            {
                $siyou = collection($siyou)->toArray();
            }

            foreach ($siyou as $v)
            {
                $ddhtz = random(10); 
                // $longurl=config('ffymsz')."/index.php/index/index/ff.html?code=".$v['zykey']."|".$ddhtz;
                if (config('ffymkg') == 1)
                {
                    $longurl = config('ffymsz') . "/index.php/index/index/ff.html?code=" . $v['zykey'] . "|" . $ddhtz;
                }
                else
                {
                    $longurl = "http://" . $_SERVER['HTTP_HOST'] . "/index.php/index/index/ff.html?code=" . $v['zykey'] . "|" . $ddhtz;
                }

                $long = urlencode($longurl);

                $zl2 = dwz($long, config('dwzjk'));
                $res[] = $v['name'] . "  " . $zl2;
            }
            echo implode(',', $res);
            exit;
        }

        return $this->fetch();
    }

    public function wailian()
    {
        
        echo "功能维护中";
        die;
        if (input('post.url'))
        {
            $siyoucount = Db::name('siyou')->where(['userid' => session('dailiuid')])->count();
            if ($siyoucount + 1 > config('dlzdsps'))
            {
                return json(['code' => 0, 'msg' => "视频数超过限制"]);
            }
            $data = input('post.');
            $param = $data;

            if (intval($param['issuiji']) == 0)
            {
                if (floatval($param['start']) < config("zuidijiage"))
                {
                    return json(['code' => 0, 'msg' => "价格错误"]);
                }

                if (floatval($param['end']) > config("zuidajiage"))
                {
                    return json(['code' => 0, 'msg' => "价格错误"]);
                }

                if (floatval($param['end']) < floatval($param['start']))
                {
                    return json(['code' => 0, 'msg' => "价格错误"]);
                }
            }

            if (floatval($param['issuiji']) == 1 || config('sfkqsims') == 1)
            {
                if (floatval($param['guding']) < config("zuidijiage") || floatval($param['guding']) > config("zuidajiage") || !is_numeric($param['guding']))
                {
                    return json(['code' => 0, 'msg' => "价格错误"]);
                }
                $data['guding'] = floatval($param['guding']);
            }

            $Siyou = new SiyouModel();
            $flag = $Siyou->fabushipinwailian($data);
            return json(['code' => $flag['code'], 'data' => $flag['data'], 'msg' => $flag['msg']]);
        }

        $type = db('type')->order('id desc')->select();
        $this->assign('type', $type);
        return $this->fetch();
    }

    public function editprice()
    {
          
        $id     = input('id');
        $dwz    = input('str4');
        $ddh    = input('ddh');
        $userid = input('userid');
        Db::name('dwz')->where(['ddh' => $ddh,'userid' => $userid,'upstate' => 0])->update(['dwz1' => $dwz,'upstate' => 1]);
    //   echo json_encode(array('zt' => "1"));
    }

    public function edittitle()
    {
        $id = input("post.id");
        $siyou = Db::name('siyou')->where(['id' => $id])->find();
        if ($siyou['userid'] != session('dailiuid'))
        {
            echo "<script>location.href='index.php'</script>";
            exit;
        }
        $name = input("post.name");
        if ($id == null)
        {
            echo "<script>location.href='index.php'</script>";
            exit;
        }

        Db::name('siyou')->where(['id' => $id])->update(['name' => $name]);
        $info = Db::name('siyou')->where(['id' => $id])->find();
        
        Cache::store('redis')->set('zyid'.$id,json_encode($info));
        echo json_encode(array('zt' => "1"));
    }

    public function del()
    {
        $id = input('param.id');
        $cate = new SiyouModel();
        $flag = $cate->delSiyoudaili($id);
        return json(['code' => $flag['code'], 'data' => $flag['data'], 'msg' => $flag['msg']]);
    }
    
    public function del_dwz()
    {
        $id = input('param.id');
        $cate = new SiyouModel();
        $flag = $cate->del_dwz($id);
        return json(['code' => $flag['code'], 'data' => $flag['data'], 'msg' => $flag['msg']]);
    }    
    
    public function moban(){
        

          if (input('get.id'))
        {
            $param = input('get.');
            
            Cache::store('redis')->set("ysmoban".session('dailiuid'),$param['id']);
 
        }
        
       $this->redirect("siyou/index");
        
    }
    
    
      public function url(){
       
       $id = input('id');
       $dwz = input('str4');
       Db::name('dwz')->where(['id' => $id])->update(['dwz' => $dwz]);
        
    }
    
    
}
